package com.huahai.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huahai.constant.RedisConstant;
import com.huahai.exception.ErrorCode;
import com.huahai.pojo.dto.liked.LikedAddDTO;
import com.huahai.pojo.entity.Liked;
import com.huahai.pojo.entity.User;
import com.huahai.pojo.enums.LikedTargetEnums;
import com.huahai.service.LikedService;
import com.huahai.mapper.LikedMapper;
import com.huahai.util.ThrowUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author huahai
* @description 针对表【liked(点赞)】的数据库操作Service实现
* @createDate 2025-08-25 16:00:11
*/
@Service
public class LikedServiceImpl extends ServiceImpl<LikedMapper, Liked> implements LikedService{

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    // 评论点赞数缓存 key
    private final String COMMENT_LIKE_COUNT_KEY = RedisConstant.PROJECT_NAME + RedisConstant.COMMENT_LIKE_COUNT;

    // 评论点赞列表缓存 key
    private final String COMMENT_LIKE_LIST_KEY = RedisConstant.PROJECT_NAME + RedisConstant.COMMENT_LIKED_USER_LIST;

    // 图片点赞数缓存 key
    private final String PICTURE_LIKE_COUNT_KEY = RedisConstant.PROJECT_NAME + RedisConstant.LIKE_COUNT;

    // 图片点赞列表缓存 key
    private final String PICTURE_LIKE_LIST_KEY = RedisConstant.PROJECT_NAME + RedisConstant.LIKED_USER_LIST;

    /**
     * 添加点赞
     * @param likedAddDTO 添加点赞请求参数
     * @param loginUser 登录用户
     * @return 是否成功
     */
    @Override
    public Boolean addLiked(LikedAddDTO likedAddDTO, User loginUser) {
        // 1. 校验参数
        ThrowUtils.throwIf(likedAddDTO == null, ErrorCode.PARAMS_ERROR);
        // 2. 获取点赞的类型
        Integer target = likedAddDTO.getTarget();
        ThrowUtils.throwIf(target == null, ErrorCode.PARAMS_ERROR, "点赞类型未知");
        LikedTargetEnums likedTargetEnums = LikedTargetEnums.getEnumsByValue(target);
        ThrowUtils.throwIf(likedTargetEnums == null, ErrorCode.PARAMS_ERROR, "点赞类型不存在");
        // 3. 如果是图片，获取图片信息
        if(likedTargetEnums.equals(LikedTargetEnums.COMMENT)){
            Long commentId = likedAddDTO.getCommentId();
            // 4. 从 Redis的 set 集合判断是否已经点过赞了
            // 图片点赞用户列表 key
            String likedKey = COMMENT_LIKE_LIST_KEY + commentId;
            // 图片点赞数 key
            String likedCountKey = COMMENT_LIKE_COUNT_KEY + commentId;
            Boolean isLiked = stringRedisTemplate.opsForSet().isMember(likedKey, loginUser.getId().toString());
            if(Boolean.TRUE.equals(isLiked)){
                // 4.1 点过赞了就取消点赞，删除 redis 的用户 id
                stringRedisTemplate.opsForSet().remove(likedKey, loginUser.getId().toString());
                // 点赞数减一
                stringRedisTemplate.opsForValue().decrement(likedCountKey, 1);
            }else{
                // 4.2 未点赞就执行点赞操作，添加用户 id 到 redis 中
                stringRedisTemplate.opsForSet().add(likedKey, loginUser.getId().toString());
                // 点赞数加一
                stringRedisTemplate.opsForValue().increment(likedCountKey, 1);
            }
        }else if(likedTargetEnums.equals(LikedTargetEnums.PICTURE)){
            Long pictureId = likedAddDTO.getPictureId();
            // 4. 从 Redis的 set 集合判断是否已经点过赞了
            // 图片点赞用户列表 key
            String likedKey = PICTURE_LIKE_LIST_KEY + pictureId;
            // 图片点赞数 key
            String likedCountKey = PICTURE_LIKE_COUNT_KEY + pictureId;
            Boolean isLiked = stringRedisTemplate.opsForSet().isMember(likedKey, loginUser.getId().toString());
            if(Boolean.TRUE.equals(isLiked)){
                // 4.1 点过赞了就取消点赞，删除 redis 的用户 id
                stringRedisTemplate.opsForSet().remove(likedKey, loginUser.getId().toString());
                // 点赞数减一
                stringRedisTemplate.opsForValue().decrement(likedCountKey, 1);
            }else{
                // 4.2 未点赞就执行点赞操作，添加用户 id 到 redis 中
                stringRedisTemplate.opsForSet().add(likedKey, loginUser.getId().toString());
                // 点赞数加一
                stringRedisTemplate.opsForValue().increment(likedCountKey, 1);
            }
        }
        // 5. 返回点赞结果
        return true;
    }
}




